/*
   Copyright (c) 2010 Bryce Alcock

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.com

 */

package com.ecocrypt.timeutils;

/**
 * Some simple Time/date formatting tools.
 * @author bryce.alcock
 */
public class Timeformatting {
    /**
     * This will take a long duration, and return a StringBuilder
     * with the format of xxD yyH mmM ssS sssMS, where the values that don't
     * exist will be completely omitted.
     * <B/>
     * E.G.<br/>  durationToString(434) would return a StringBuilder containing "434ms"
     * <br/>durationToString(1888) would return "1S 434ms"
     *
     * @param dur
     * @return StringBuilder representation of the duration.
     */
    public static StringBuilder durationToString(long dur) {
        int millis = (int) dur % 1000;
        dur = dur / 1000;
        int sec = (int) dur % 60;
        dur = dur / 60;
        int min = (int) dur % 60;
        dur = dur / 60;
        int hr = (int) dur % 24;
        int days = (int) dur / 24;
        StringBuilder sb = new StringBuilder();
        sb = (days > 0) ? sb.append(days).append("D ") : sb;
        sb = (hr > 0) ? sb.append(hr).append("H ") : sb;
        sb = (min > 0) ? sb.append(min).append("M ") : sb;
        sb = (sec > 0) ? sb.append(sec).append("S ") : sb;
        sb.append(millis).append("ms");
        return sb;
    }

}
